// 商城订单
import { get, post } from '@common/ajax';
import logger from '@common/logger';
import { SHOP_URL } from '@common/url';

import {
    GET_ORDER_LIST,
    SET_ORDER_LIST_PARAM,
    SET_AFTER_SALE_ORDER_LIST_PARAM,
    GET_AFTER_SALE_ORDER_LIST,
} from '@store/mutationType';

const orderList = {
    state: {
        orderList: [],
        // 查询相关
        categoryId: 0,
        searchValue: '',
        pageNumber: 1,
        pageSize: 10,
        // 售后相关
        afterSaleOrderList: [],
        afterPageNumber: 1,
        afterPageSize: 10,
    },
    mutations: {
        SET_ORDER_LIST_PARAM: (state, { categoryId, pageNumber }) => {
            Object.assign(state, { categoryId, pageNumber });
        },
        GET_ORDER_LIST: (state, orderList) => {
            Object.assign(state, { orderList });
        },
        SET_AFTER_SALE_ORDER_LIST_PARAM: (state, { afterPageNumber, afterPageSize }) => {
            Object.assign(state, {
                afterPageNumber,
                afterPageSize,
            });
        },
        GET_AFTER_SALE_ORDER_LIST: (state, afterSaleOrderList) => {
            Object.assign(state, { afterSaleOrderList });
        },
    },
    actions: {
        // 查询组合单摘要
        getUniteOrderAbstract: async ({ commit }, requestId) => {
            const res = await get(SHOP_URL.orderPrepay, { request_id: requestId });
            logger.success('获取的摘要信息为：', res);
            return res;
        },
        // 重新唤起支付
        repayUniteOrderAction: async ({ commit }, param) => {
            const res = await post(SHOP_URL.orderPay, param);
            logger.success('ShopOrder => 唤起支付结果', res);
            return res;
        },
        // 取消订单
        cancelUniteOrderInList: async ({ dispatch, state }, requestId) => {
            const { categoryId } = state;
            const res = await post(SHOP_URL.orderCancel, { request_id: requestId });
            logger.success('取消组合单（含支付信息）结果 => ', res);
            if (res) {
                dispatch('getOrderList', { categoryId });
                return true;
            }
            return false;
        },
        // 获取订单列表
        getOrderList: async ({ commit }, { categoryId, searchValue }) => {
            const res = await get(SHOP_URL.orderList, {
                category: categoryId,
                query: searchValue,
                page_no: 1,
                page_size: 10,
            });
            commit(SET_ORDER_LIST_PARAM, {
                categoryId,
                pageNumber : 1,
            });
            commit(GET_ORDER_LIST, res.orders);
        },
        // 获取下一页
        getOrderListNext: async ({ commit, state }) => {
            const { categoryId, searchValue, pageNumber, pageSize, orderList } = state;
            const pageNo = pageNumber + 1;
            const res = await get(SHOP_URL.orderList, {
                category: categoryId,
                query: searchValue,
                page_no: pageNo,
                page_size: pageSize,
            });
            commit(SET_ORDER_LIST_PARAM, {
                categoryId,
                pageNumber: pageNo,
            });
            commit(GET_ORDER_LIST, [ ...orderList, ...res.orders ]);
        },
        // 获取售后列表
        getAfterSaleOrderList: async ({ commit }) => {
            const page_no = 1;
            const page_size = 10;
            const res = await get(SHOP_URL.refundList, {
                page_no,
                page_size,
            });
            logger.success('ShopOrderList => AfterSale', res.applies);
            commit(SET_AFTER_SALE_ORDER_LIST_PARAM, {
                afterPageNumber: page_no,
                afterPageSize: page_size,
            });
            commit(GET_AFTER_SALE_ORDER_LIST, res.applies);
        },
        // 获取售后列表下一页
        getAfterSaleOrderListNext: async ({ commit, state }) => {
            const { afterPageNumber, afterPageSize, afterSaleOrderList } = state;
            const pageNumber = afterPageNumber + 1;
            const res = await get(SHOP_URL.refundList, {
                page_no: pageNumber,
                page_size: afterPageSize,
            });
            commit(SET_AFTER_SALE_ORDER_LIST_PARAM, {
                afterPageNumber: pageNumber,
                afterPageSize,
            });
            const list = [...afterSaleOrderList, ...res.applies];
            commit(GET_AFTER_SALE_ORDER_LIST, list);
        },
    },
};

export default orderList;
